/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.profile;

import org.openaecfm.model.profile.AsymmetricIShapeProfileDef;
import org.openaecfm.model.profile.ParameterizedProfileDef;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class AsymmetricIShapeProfileDefIO extends
		AbstractEntityInstanceInputImpl<AsymmetricIShapeProfileDef> implements
		IEntityInstanceOutput<AsymmetricIShapeProfileDef> {

	private IEntityInstanceUtilInput<ParameterizedProfileDef> utilInput;
	private IEntityInstanceUtilOutput<ParameterizedProfileDef> utilOutput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<ParameterizedProfileDef> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(AsymmetricIShapeProfileDef pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCASYMMETRICISHAPEPROFILEDEF", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set BottomFlangeWidth
		ei.addParameter(new RealParameter(pojo.getBottomFlangeWidth()));

		// Set OverallDepth
		ei.addParameter(new RealParameter(pojo.getOverallDepth()));

		// Set WebThickness
		ei.addParameter(new RealParameter(pojo.getWebThickness()));

		// Set BottomFlangeThickness
		ei.addParameter(new RealParameter(pojo.getBottomFlangeThickness()));

		// Set BottomFlangeFilletRadius
		if (pojo.getBottomFlangeFilletRadius() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo
					.getBottomFlangeFilletRadius()));
		}

		// Set TopFlangeWidth
		ei.addParameter(new RealParameter(pojo.getTopFlangeWidth()));

		// Set TopFlangeThickness
		if (pojo.getTopFlangeThickness() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getTopFlangeThickness()));
		}

		// Set TopFlangeFilletRadius
		if (pojo.getTopFlangeFilletRadius() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getTopFlangeFilletRadius()));
		}

		// Set BottomFlangeEdgeRadius
		if (pojo.getBottomFlangeEdgeRadius() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getBottomFlangeEdgeRadius()));
		}

		// Set BottomFlangeSlope
		if (pojo.getBottomFlangeSlope() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getBottomFlangeSlope()));
		}

		// Set TopFlangeEdgeRadius
		if (pojo.getTopFlangeEdgeRadius() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getTopFlangeEdgeRadius()));
		}

		// Set TopFlangeSlope
		if (pojo.getTopFlangeSlope() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getTopFlangeSlope()));
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected AsymmetricIShapeProfileDef doCreateObject() {
		return new AsymmetricIShapeProfileDef();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			AsymmetricIShapeProfileDef pojo, IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<ParameterizedProfileDef> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			AsymmetricIShapeProfileDef pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set BottomFlangeWidth
		RealParameter bottomFlangeWidth = (RealParameter) sei.getParameter(3);
		pojo.setBottomFlangeWidth(bottomFlangeWidth.toDouble());

		// Set OverallDepth
		RealParameter overallDepth = (RealParameter) sei.getParameter(4);
		pojo.setOverallDepth(overallDepth.toDouble());

		// Set WebThickness
		RealParameter webThickness = (RealParameter) sei.getParameter(5);
		pojo.setWebThickness(webThickness.toDouble());

		// Set BottomFlangeThickness
		RealParameter bottomFlangeThickness = (RealParameter) sei
				.getParameter(6);
		pojo.setBottomFlangeThickness(bottomFlangeThickness.toDouble());

		// Set BottomFlangeFilletRadius
		Parameter bottomFlangeFilletRadius = sei.getParameter(7);
		if (bottomFlangeFilletRadius.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) bottomFlangeFilletRadius;
			pojo.setBottomFlangeFilletRadius(r.toDouble());
		}

		// Set TopFlangeWidth
		RealParameter topFlangeWidth = (RealParameter) sei.getParameter(8);
		pojo.setTopFlangeWidth(topFlangeWidth.toDouble());

		// Set TopFlangeThickness
		Parameter topFlangeThickness = sei.getParameter(9);
		if (topFlangeThickness.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) topFlangeThickness;
			pojo.setTopFlangeThickness(r.toDouble());
		}

		// Set TopFlangeFilletRadius
		Parameter topFlangeFilletRadius = sei.getParameter(10);
		if (topFlangeFilletRadius.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) topFlangeFilletRadius;
			pojo.setTopFlangeFilletRadius(r.toDouble());
		}

		// Set BottomFlangeEdgeRadius
		Parameter bottomFlangeEdgeRadius = sei.getParameter(11);
		if (bottomFlangeEdgeRadius.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) bottomFlangeEdgeRadius;
			pojo.setBottomFlangeEdgeRadius(r.toDouble());
		}

		// Set BottomFlangeSlope
		Parameter bottomFlangeSlope = sei.getParameter(12);
		if (bottomFlangeSlope.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) bottomFlangeSlope;
			pojo.setBottomFlangeSlope(r.toDouble());
		}

		// Set TopFlangeEdgeRadius
		Parameter topFlangeEdgeRadius = sei.getParameter(13);
		if (topFlangeEdgeRadius.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) topFlangeEdgeRadius;
			pojo.setTopFlangeEdgeRadius(r.toDouble());
		}

		// Set TopFlangeSlope
		Parameter topFlangeSlope = sei.getParameter(14);
		if (topFlangeSlope.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) topFlangeSlope;
			pojo.setTopFlangeSlope(r.toDouble());
		}
	}
}
